<?php
namespace app\model;

class Project extends BaseModel
{
    protected $name = 'projects';

    // 自动时间戳
    protected $autoWriteTimestamp = true;

    // 类型转换
    protected $type = [
        'created_at' => 'integer',
        'updated_at' => 'integer',
        'delete_time' => 'integer',
        'start_date' => 'datetime',
        'plan_end_date' => 'datetime'
    ];

    // 项目状态常量
    const STATUS_ONGOING = 1;   // 进行中
    const STATUS_COMPLETED = 2; // 已完成
    const STATUS_PAUSED = 3;    // 已暂停

    // 状态映射
    public static $statusMap = [
        self::STATUS_ONGOING => '进行中',
        self::STATUS_COMPLETED => '已完成',
        self::STATUS_PAUSED => '已暂停'
    ];

    /**
     * 设置计划时长（天）
     * @param int $days 计划天数
     */
    public function setDuration($days)
    {
        if (empty($this->start_date)) {
            throw new \Exception('请先设置项目开始时间');
        }

        $startDate = new \DateTime($this->start_date);
        $endDate = clone $startDate;
        $endDate->modify("+{$days} days");
        
        $this->plan_end_date = $endDate->format('Y-m-d H:i:s');
        return $this;
    }

    /**
     * 获取计划时长（天）
     * @return int
     */
    public function getDuration()
    {
        if (empty($this->start_date) || empty($this->plan_end_date)) {
            return 0;
        }

        $startDate = new \DateTime($this->start_date);
        $endDate = new \DateTime($this->plan_end_date);
        
        // 计算日期差
        $interval = $startDate->diff($endDate);
        return $interval->days;
    }

    /**
     * 获取剩余天数
     * @return int
     */
    public function getRemainingDays()
    {
        if (empty($this->plan_end_date)) {
            return 0;
        }

        $now = new \DateTime();
        $endDate = new \DateTime($this->plan_end_date);
        
        if ($now > $endDate) {
            return 0;
        }

        $interval = $now->diff($endDate);
        return $interval->days;
    }

    /**
     * 获取项目进度百分比
     * @return float
     */
    public function getProgress()
    {
        if (empty($this->start_date) || empty($this->plan_end_date)) {
            return 0;
        }

        $startDate = new \DateTime($this->start_date);
        $endDate = new \DateTime($this->plan_end_date);
        $now = new \DateTime();

        // 如果还未开始
        if ($now < $startDate) {
            return 0;
        }

        // 如果已超期
        if ($now > $endDate) {
            return 100;
        }

        // 计算总天数和已过天数
        $totalDays = $startDate->diff($endDate)->days;
        $passedDays = $startDate->diff($now)->days;

        if ($totalDays == 0) {
            return 100;
        }

        return round(($passedDays / $totalDays) * 100, 2);
    }

    /**
     * 检查项目是否已超期
     * @return bool
     */
    public function isOverdue()
    {
        if (empty($this->plan_end_date)) {
            return false;
        }

        return new \DateTime() > new \DateTime($this->plan_end_date);
    }

    /**
     * 关联用户
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * 关联提醒
     */
    public function reminders()
    {
        return $this->hasMany(Reminder::class);
    }

    /**
     * 获取状态文本
     */
    public function getStatusTextAttr()
    {
        return self::$statusMap[$this->status] ?? '未知状态';
    }

    /**
     * 获取项目时间状态
     */
    public function getTimeStatusAttr()
    {
        if ($this->duration <= 0) {
            return '未设置计划时长';
        }

        $endDate = strtotime("+{$this->duration} days", strtotime($this->start_date));
        $now = time();
        
        if ($now < strtotime($this->start_date)) {
            return '未开始';
        } elseif ($now > $endDate) {
            return '已超期';
        } else {
            return '进行中';
        }
    }
} 